import java.util.Random;

//1.继承 Thread, 重写 run
class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("hello thread");
    }
}
//2.实现 Runnable, 重写 run
class MyThread2 implements Runnable {
    @Override
    public void run() {
        System.out.println("hello thread");
    }
}
//继承 Thread, 重写 run, 使用匿名内部类
//public class Test {
//    public static void main(String[] args) {
//        Thread t = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("hello thread");
//            }
//        });
//        t.start();
//    }
//}

public class Test {
    private static long sum1 = 0;
    private static long sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        int[] arr = new int[1_0000_0000];
        // 给数组赋随机值
        Random random = new Random(System.currentTimeMillis());
        for(int i = 0; i<1_0000_0000; i++) {
            arr[i] = random.nextInt(100);
        }

        //求偶数下标的和
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i<1_0000_0000; i+=2) {
                    sum1 += arr[i];
                }
            }
        });


        //求偶数下标的和
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 1; i<1_0000_0000; i+=2) {
                    sum2 += arr[i];
                }
            }
        });
        long beg = System.currentTimeMillis();
        t1.start();
        t2.start();

        t1.join();
        t2.join();
        long end = System.currentTimeMillis();

        System.out.println("sum = " + (sum1+sum2) +", time:" + (end-beg));
    }
}